<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="88px"
        inline
      >
        <el-form-item label="年龄" prop="param.age">
          <!-- <el-input-number
            style="width: 240px"
            @keyup.enter.native="handleQuery"
            v-model="queryParams.param.age"
            placeholder="请输入年龄"
            :precision="0"
            :controls="false"
            :min="0"
          ></el-input-number> -->
          <el-input
            :controls="false"
            v-model="queryParams.param.age"
            type="number"
            placeholder="请输入年龄"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="性别" prop="param.sex">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.sex"
            placeholder="请选择性别"
          >
            <el-option
              v-for="item in dict.type.sys_user_sex"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职业" prop="param.career">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.career"
            placeholder="请选择"
          >
            <el-option
              v-for="(value, key, index) in career"
              :label="value"
              :value="key"
              :key="index"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['definition:class:download']"
            @click="downloadData"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" fixed />
        <el-table-column
          prop="gender"
          label="性别"
          width="180"
          align="center"
          fixed
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_user_sex"
              :value="scope.row.gender"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="maxAge"
          label="最大年龄"
          width="180"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="minAge"
          label="最小年龄"
          width="180"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="path"
          label="画像地址"
          width="180"
          align="center"
          fixed
        >
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="baseUrl + scope.row.path"
              :preview-src-list="[baseUrl + scope.row.path]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="qualification"
          label="学历"
          width="180"
          align="center"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="scope.row.qualification == 0">小学及以下</span>
            <span v-else-if="scope.row.qualification == 1">初中</span>
            <span v-else-if="scope.row.qualification == 2">中专/高中</span>
            <span v-else-if="scope.row.qualification == 3">大专/大本</span>
            <span v-else-if="scope.row.qualification == 4">硕士及以上</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          width="180"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            {{ $moment(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建人" align="center">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="最后修改"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            {{ $moment(scope.row.updateTime).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="updateBy" label="修改人" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="160">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改部门对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="800px"
      append-to-body
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="性别" prop="gender">
              <el-select
                style="width: 240px"
                v-model="addForm.gender"
                placeholder="请选择性别"
              >
                <el-option
                  v-for="item in dict.type.sys_user_sex"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value - 0"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="职业" prop="job">
              <el-select
                style="width: 240px"
                v-model="addForm.job"
                placeholder="请选择"
              >
                <el-option
                  v-for="(value, key, index) in career"
                  :label="value"
                  :value="key - 0"
                  :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="最大年龄" prop="maxAge">
              <el-input-number
                v-model="addForm.maxAge"
                :min="1"
                label="最大年龄"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="最小年龄" prop="minAge">
              <el-input-number
                v-model="addForm.minAge"
                :min="1"
                label="最小年龄"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="画像地址" prop="path">
              <imageUpload :limit="1" v-model="addForm.path" :fileSize="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="学历" prop="qualification">
              <el-select
                style="width: 240px"
                v-model="addForm.qualification"
                placeholder="请选择学历"
              >
                <el-option label="小学及以下" :value="0"> </el-option>
                <el-option label="初中" :value="1"> </el-option>
                <el-option label="中专/高中" :value="2"> </el-option>
                <el-option label="大专/大本" :value="3"> </el-option>
                <el-option label="硕士及以上" :value="4"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark"
              ><el-input
                type="textarea"
                :rows="2"
                placeholder="备注"
                v-model="addForm.remark"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listPmsPortrait,
  insertPmsPortrait,
  updatePmsPortrait,
  deletePmsPortrait,
  exportData,
} from "@/api/system/portrait";
import { getListCode } from "@/api/business/definition";
export default {
  dicts: ["sys_user_sex"],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          text: "",
          code: "",
          tagIdList: [],
        },
      },
      tagList: [],
      dataList: [],
      total: 0,
      title: "",
      addDialog: false,
      addForm: {
        name: "",
      },
      rules: {
        gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
        job: [{ required: true, message: "请选择职业", trigger: "blur" }],
        maxAge: [
          { required: true, message: "请输入最大年龄", trigger: "blur" },
        ],
        minAge: [
          { required: true, message: "请输入最小年龄", trigger: "blur" },
        ],
        path: [{ required: true, message: "请选择图片", trigger: "blur" }],
      },
      career: {},
    };
  },
  created() {
    this.getList();
    this.getCAREER();
  },
  methods: {
    getCAREER() {
      const queryParams = {
        pageNum: 1,
        pageSize: 1,
        param: {
          code: "FILES-CAREER",
        },
      };
      getListCode(queryParams).then((res) => {
        this.career = JSON.parse(res.data.list[0].options);
        console.log("this.career: ", this.career);
      });
    },
    // 导出数据
    downloadData() {
      const that = this;
      this.$modal
        .confirm("是否确认导出当前查询条件下的数据？")
        .then(function () {
          // 导出
          const params = that.queryParams;
          return that.download(
            exportData,
            { ...params.param },
            `画像_${new Date().getTime()}.xlsx`
          );
        })
        .then((res) => {
          that.$message.success("导出成功");
        })
        .catch(() => {});
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const param = { ...this.queryParams };
      listPmsPortrait(param).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增画像
    handleAdd() {
      this.title = "新增画像";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    submitForm() {
      this.$refs["addForm"].validate(async (valid) => {
        if (valid) {
          const param = { ...this.addForm };
          if (param.id) {
            // 修改
            await updatePmsPortrait(param).then((res) => {
              this.$message.success("修改成功");
              this.cancel();
              this.handleQuery();
            });
          } else {
            // 新增
            await insertPmsPortrait(param).then((res) => {
              this.$message.success("新增成功");
              this.cancel();
              this.handleQuery();
            });
          }
        } else {
          return false;
        }
      });
    },
    arrayToObject(arr) {
      return arr.reduce((accumulator, currentValue, currentIndex) => {
        accumulator[currentIndex + 1] = currentValue;
        return accumulator;
      }, {});
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        text: "",
      };
      this.addDialog = false;
    },
    // 修改变量
    handleEdit(row) {
      this.title = "修改变量";
      const data = row.id ? row : this.selectionData[0];
      this.addForm = {
        ...data,
      };
      this.addDialog = true;
    },
    // 删除变量
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的画像？")
        .then(function () {
          // 删除
          return deletePmsPortrait({ idList });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss">
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.row-1 {
  width: 20%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.row-3 {
  width: 80%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
<style scoped>
/deep/ input::-webkit-outer-spin-button,
/deep/ input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
/deep/ input[type="number"] {
  -moz-appearance: textfield;
}
</style>
